package cn.new7as.cas5s.ui.repay;


import android.annotation.SuppressLint;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.hjq.http.EasyHttp;
import com.hjq.http.listener.OnHttpListener;

import java.util.List;

import cn.new7as.cas5s.tool.util.Utils;
import cn.new7as.cas5s.ui.easy.netapi.PayChannelApi;
import cn.new7as.cas5s.ui.easy.netapi.PayUrlApi;
import cn.new7as.cas5s.ui.slideshow.BaseActivity;
import cn.new7as.cas5s.databinding.ActivityOrdersBinding;
import cn.new7as.cas5s.ui.easy.netapi.HttpData;
import cn.new7as.cas5s.ui.easy.netapi.OrderApi;


public class OrdersActivity extends BaseActivity {


    private ActivityOrdersBinding binding;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityOrdersBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setop("Orders");
        initV();
        //

    }

    @Override
    public void initV() {
        super.initV();
        show();
        getorder();
    }

    private void getorder() {
        JsonMap jsonMap = new JsonMap();
        jsonMap.set("pageNo", "0");
        HttpRequest.build(this, "http://16.163.9.142/cbf-app/message/smsSend")
                .addHeaders("", "")
                .setJsonParameter(jsonMap)
                .setResponseListener(new ResponseListener() {
                    @Override
                    public void onResponse(String response, Exception error) {
                        Log.e("aaaa", response.toString());
                    }
                })
                .doPost();


        EasyHttp.post(this)
                .api(new OrderApi(getunique(), 0)).request(new OnHttpListener<HttpData<OrderApi.OrderData>>() {

                    @Override
                    public void onHttpSuccess(@NonNull HttpData<OrderApi.OrderData> result) {

                        hide();
                        binding.ordersgride.setAdapter(new Ordersadapter(result.getResult().repaymentRecords, OrdersActivity.this));
                    }

                    @Override
                    public void onHttpFail(@NonNull Throwable throwable) {
                        deal(throwable);
                    }
                });
    }


    public void getChanel(OrderApi.Data data, int type) {
        show();
        EasyHttp.get(this).api(new PayChannelApi(getunique(), data.tradeNo)).request(new OnHttpListener<HttpData<List<PayChannelApi.Data>>>() {


            @Override
            public void onHttpSuccess(@NonNull HttpData<List<PayChannelApi.Data>> result) {
                List<PayChannelApi.Data> result1 = (List<PayChannelApi.Data>) result.getResult();
                if (!result1.isEmpty()) {
                    EasyHttp.post(OrdersActivity.this).api(new PayUrlApi(getunique(), result1.get(0).code, type, data.tradeNo)).request(new OnHttpListener<HttpData<String>>() {


                        @Override
                        public void onHttpSuccess(@NonNull HttpData<String> result) {
                            Utils.goextra(OrdersActivity.this, PayForthActivity.class, result.getResult());
                            hide();
                        }


                        @Override
                        public void onHttpFail(@NonNull Throwable throwable) {
                            deal(throwable);
                        }
                    });

                }


            }

            @Override
            public void onHttpFail(@NonNull Throwable throwable) {
                deal(throwable);
            }
        });
    }


}